package com.mjt.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mjt.admin.mapper.RoleMenuMapper;
import com.mjt.admin.mapper.UserMapper;
import com.mjt.admin.model.RespDataBean;
import com.mjt.admin.pojo.Role;
import com.mjt.admin.mapper.RoleMapper;
import com.mjt.admin.pojo.RoleMenu;
import com.mjt.admin.query.RoleQuery;
import com.mjt.admin.service.IRoleMenuService;
import com.mjt.admin.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjt.admin.utils.AssertUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 服务实现类
 * </p>
 *
 * @author 莫某人
 * @since 2021-10-27
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements IRoleService {


    @Resource
    private IRoleMenuService roleMenuService;
    /**
     * @description 查询角色列表
     * @author 莫某人
     * @date 17:01 2021/10/28
     * @param roleQuery
     * @return {@link }
     **/
    @Override
    public RespDataBean<Role> roleList(RoleQuery roleQuery) {
        IPage<Role> page = new Page<Role>(roleQuery.getPage(),roleQuery.getLimit());
        QueryWrapper<Role> wrapper = new QueryWrapper<>();
        wrapper.eq("is_del",0);
        if (StringUtils.isNotBlank(roleQuery.getRoleName())){
            wrapper.like("name",roleQuery.getRoleName());
        }
        page = this.baseMapper.selectPage(page, wrapper);
        RespDataBean<Role> result = new RespDataBean<Role>(0, "查询成功", page.getRecords(), page.getTotal());
        return result;

    }

    /**
     * @description 添加角色
     * @author 莫某人
     * @date 18:13 2021/10/28
     * @param role
     * @return void
     **/
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void saveRole(Role role) {
        AssertUtil.isTrue(StringUtils.isEmpty(role.getName()),"角色名不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(role.getBz()),"备注名不能为空");
        AssertUtil.isTrue(!save(role),"添加失败");
    }

    /**
     * @description 修改角色信息
     * @author 莫某人
     * @date 18:27 2021/10/28
     * @param role
     * @return {@link }
     **/

    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void updateRole(Role role) {
        AssertUtil.isTrue(StringUtils.isEmpty(role.getName()),"角色名不能为空");
        AssertUtil.isTrue(StringUtils.isEmpty(role.getBz()),"备注名不能为空");


        AssertUtil.isTrue(!updateById(role),"角色信息修改失败");

    }

    /**
     * @description 删除角色
     * @author 莫某人
     * @date 18:37 2021/10/28
     * @param id
     * @return void
     **/
    @Override
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
    public void deleteRole(Integer id) {
        Role role = new Role();
        role.setId(id);
        role.setIsDel(1);
        AssertUtil.isTrue(!updateById(role),"删除失败");
    }

    /**
     * @description 获取角色列表
     * @author 莫某人
     * @date 19:24 2021/10/28
 * @param userId
     * @return {@link List< Map< String, Object>>}
     **/

    @Override
    public List<Map<String, Object>> queryAllRoles(Integer userId) {

        return this.baseMapper.queryAllRoles(userId);

    }

    /**
     * @description 角色授权
     * @author 莫某人
     * @date 13:54 2021/10/29
 * @param roleId
     * @param mids
     * @return {@link }
     **/
    @Override
    public void addGrant(Integer roleId, Integer[] mids) {

        AssertUtil.isTrue(roleId==null,"角色不能为空");

        int count = roleMenuService.count(new QueryWrapper<RoleMenu>().eq("role_id", roleId));
        if (count>0){
                AssertUtil.isTrue(!roleMenuService.remove(new QueryWrapper<RoleMenu>().eq("role_id",roleId)),"删除失败");
        }
        if (mids==null||mids.length==0) {
            //如果minds为空,删除角色的所有授权
                AssertUtil.isTrue(!roleMenuService.remove(new QueryWrapper<RoleMenu>().eq("role_id",roleId)),"删除失败");
        }else {
            //如果不为空，删除角色所有授权，添加选择的授权
            for (Integer mid : mids) {
                AssertUtil.isTrue(!roleMenuService.save(new RoleMenu(mid,roleId)),"添加失败");
            }
        }


    }


}
